/*
 * The MIT License (MIT)
 *
 * File: adc.h
 *
 * Description: ADC library for msp430.
 *
 * Copyright (c) 2013 Ed Crampton
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "adc.h"
#include "../board.h"
#include "stdio.h"
#include <msp430.h>
#include <legacymsp430.h>

static const float ADC_REF = 1.5;
static int mTemperature = 0;//temperature to the nearest degree
volatile uint8_t gTempReady = 0;

void AdcInitialize()
{
   ADC10CTL1 = INCH_5 + ADC10DIV_7;            // Repeat single channel 5
   ADC10CTL0 = SREF_1 + ADC10SHT_3 + ADC10ON + REFON + ADC10IE; //Turn on reference out sample auto ADC10 on sref = 1.5 ref and Vss
                                                      //temperature is in 10mV per degree should never read about 1.5V (150 F)
   ADC10AE0 |= 0x20;                         // P1.5 ADC option select

   //Timer A0 Overflow
   TACTL = TASSEL_1 + MC_2 + TAIE;           // ACLK, contmode, interrupt


   return;
}

void DisplayTemperature()
{
   char buffer[20];

   snprintf(buffer,sizeof(buffer),"Temperature: %d\n",mTemperature);
   UartPuts((const char*)buffer);
   return;
}

/*void ReadAdcValue()
{
   uint16_t temp;
   float f_temp;
   int temperature;
   int remainder_temp;
   long i= 0;
   char buffer[20];

   ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
   while((ADC10CTL0 & ADC10IFG) == 0);
   temp = ADC10MEM;
   ADC10CTL0 &= ~ADC10IFG;
   snprintf(buffer,sizeof(buffer),"ADC Value: %d\n",temp);
   UartPuts((const char*)buffer);
   f_temp = ((float)temp/(float)1023) * ADC_REF;
   snprintf(buffer,sizeof(buffer),"Voltage: %d\n",(uint16_t)(f_temp*1000.0));
   UartPuts((const char*)buffer);
   f_temp = f_temp*180.0 - 58.0;
   temperature = f_temp + .5; //rounding purposes
   snprintf(buffer,sizeof(buffer),"Temperature: %d\n",temperature);
   UartPuts((const char*)buffer);
   i = 0;
   while(i < 65530) { i++; }
}*/

interrupt (ADC10_VECTOR) ADC10_ISR(void)
{
   float temp;

   temp = ((float)ADC10MEM/1023.0) * ADC_REF;
   temp = temp*180.0 - 58.0;
   mTemperature = temp + .5; //rounding (conversion to int always round down)
   gTempReady = 1;
}

interrupt (TIMER0_A1_VECTOR) TA0_isr(void)
{
   switch( TA0IV )
   {
      case  2:  break;                        // CCR1 not used
      case  4:  break;                        // CCR2 not used
      case 10:
      {
         P1OUT ^= RED_LED_PIN;                // overflow
         ADC10CTL0 |= ENC + ADC10SC; //start conversion of ADC
         break;
      }
   }

}
